from decimal import Decimal

from app.services.energy_dispatch.energy_management_input import EnergyManagementInput
# 风力发电估算（拟合）
def calculate_wind_output(wind_speed: Decimal, rated_power=Decimal("30.0")) -> Decimal:
    cut_in = Decimal("3.0")
    rated = Decimal("12.0")
    cut_out = Decimal("25.0")

    if wind_speed < cut_in or wind_speed >= cut_out:
        return Decimal("0.0")
    elif cut_in <= wind_speed < rated:
        return rated_power * ((wind_speed - cut_in) / (rated - cut_in)) ** 3
    else:
        return rated_power

def energy_management_strategy(
        input: EnergyManagementInput,
        config_id: int,
        strategy_name: str,
        time_step_hours: float = 1.0
):
    battery_energy = Decimal(str(input.battery_energy))
    load = Decimal(str(input.load))

    solar_irradiance = Decimal(str(input.solar_output))  # W/m²
    wind_speed = Decimal(str(input.wind_output))         # m/s

    # 参数
    solar_area = Decimal("500.0")  # 假设铺设了500 m²
    solar_efficiency = Decimal("0.20")  # 常见效率

    # 光伏发电功率估算
    solar_output = (solar_irradiance * solar_area * solar_efficiency) / Decimal("1000")  # kW

    # 风力发电估算（简化）
    wind_output = calculate_wind_output(wind_speed)*3

    price_grid = Decimal(str(input.price_grid))
    price_wind = Decimal(str(input.price_wind))
    price_solar = Decimal(str(input.price_solar))

    battery_capacity = Decimal(str(input.battery_capacity))
    battery_charge_efficiency = Decimal(str(input.battery_charge_efficiency))
    battery_discharge_efficiency = Decimal(str(input.battery_discharge_efficiency))
    battery_max_charge_power = Decimal(str(input.battery_max_charge_power))
    battery_max_discharge_power = Decimal(str(input.battery_max_discharge_power))

    dt = Decimal(str(time_step_hours))

    renewable_power = solar_output + wind_output
    load_after_renewable = max(load - renewable_power, Decimal('0'))

    # 放电功率：实际需要放电满足负载，不能超过最大放电功率和电池剩余容量
    max_discharge_power_possible = min(
        battery_max_discharge_power,
        battery_energy / battery_discharge_efficiency
    )
    discharge_power = min(load_after_renewable, max_discharge_power_possible)

    load_after_battery = max(load_after_renewable - discharge_power, Decimal('0'))

    battery_energy -= (discharge_power / battery_discharge_efficiency) * dt

    # 电网供电
    grid_power = load_after_battery

    # 充电功率：剩余可再生电量用于充电，不能超过电池容量和最大充电功率
    excess_renewable = max(renewable_power - load, Decimal('0'))
    available_charge_power = min(
        battery_max_charge_power,
        (battery_capacity - battery_energy) / battery_charge_efficiency
    )
    charge_power = min(excess_renewable, available_charge_power)

    battery_energy += charge_power * battery_charge_efficiency * dt

    # 限制电池能量上下限
    if battery_energy < 0:
        battery_energy = Decimal('0')
    if battery_energy > battery_capacity:
        battery_energy = battery_capacity

    # 计算成本和排放
    cost_grid = grid_power * price_grid * dt
    cost_solar = solar_output * price_solar * dt
    cost_wind = wind_output * price_wind * dt
    total_cost = cost_grid + cost_solar + cost_wind

    emission_grid = grid_power * Decimal('0.7') * dt
    emission_solar = solar_output * Decimal('0.02') * dt
    emission_wind = wind_output * Decimal('0.05') * dt
    total_emission = emission_grid + emission_solar + emission_wind

    battery_usage = (discharge_power + charge_power) * dt
    battery_usage_rate = (
        battery_usage / battery_capacity * Decimal('100')
        if battery_capacity > 0 else Decimal('0')
    )

    return {
        "config_id": config_id,
        "strategy_name": strategy_name,
        "total_cost": total_cost.quantize(Decimal('0.01')),
        "total_emission": total_emission.quantize(Decimal('0.01')),
        "battery_usage_rate": battery_usage_rate.quantize(Decimal('0.01')),
        "battery_energy": battery_energy.quantize(Decimal('0.01')),

        "solar_power": solar_output.quantize(Decimal('0.01')),
        "grid_power": grid_power.quantize(Decimal('0.01')),
        "wind_power": wind_output.quantize(Decimal('0.01')),
        "total_power": (solar_output + wind_output + grid_power).quantize(Decimal('0.01')),

        "solar_cost": cost_solar.quantize(Decimal('0.01')),
        "wind_cost": cost_wind.quantize(Decimal('0.01')),
        "grid_cost": cost_grid.quantize(Decimal('0.01')),

        "solar_emission": emission_solar.quantize(Decimal('0.01')),
        "wind_emission": emission_wind.quantize(Decimal('0.01')),
        "grid_emission": emission_grid.quantize(Decimal('0.01')),
    }
